package com.ac.scum.log.task.face.log.counsumer.stages.gamplay;

import com.ac.common.core.id.SnowflakeIdGenerator;
import com.ac.scum.log.bean.Player;
import com.ac.scum.log.bean.PlayerAlarm;
import com.ac.scum.log.bean.ServerFTPLogChest;
import com.ac.scum.log.task.face.log.counsumer.LogConsumer;
import com.ac.scum.log.task.face.log.counsumer.PlayerAlarmInterface;
import com.ac.scum.log.task.face.log.counsumer.stages.GameplayLogConsumer;
import com.ac.scum.log.task.mapper.log.ServerFTPLogChestMapper;
import com.ac.scum.log.task.utils.map.GameMapZoneUtil;
import com.ac.scum.log.task.utils.queue.FTPQueueItem;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Service
@AllArgsConstructor
public class ChestConsumer implements PlayerAlarmInterface {
    private final GameMapZoneUtil gameMapZoneUtil;
    private final SnowflakeIdGenerator snowflakeIdGenerator;
    private final ServerFTPLogChestMapper serverFTPLogChestMapper;

    public void chestConsumer(List<LogConsumer.GameLogLine> lines, FTPQueueItem ftpQueueItem, GameplayLogConsumer gameplayLogConsumer) {
        try {
            Pattern pattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[LogChest\\] \\[(.*?)\\] ChestId: (\\d+) Owner: (.*?) \\((\\d+), (.+?)\\) Location: X=([\\d.-]+) Y=([\\d.-]+) Z=([\\d.-]+) (Burier|Unburier): (.*?) \\((\\d+), (.+?)\\)");
            List<ServerFTPLogChest> chests = Lists.newArrayList();
            for (LogConsumer.GameLogLine line : lines) {
                String lineContext = line.getLineContext();
                Matcher matcher = pattern.matcher(lineContext);
                if (matcher.find()) {
                    ServerFTPLogChest serverFTPLogChest = new ServerFTPLogChest();
                    serverFTPLogChest.setId(snowflakeIdGenerator.nextId());
                    serverFTPLogChest.setFileId(ftpQueueItem.getFileId());
                    serverFTPLogChest.setServerId(ftpQueueItem.getServer().getId());
                    serverFTPLogChest.setLineStart(line.getStartByte());
                    serverFTPLogChest.setLineEnd(line.getEndByte());
                    serverFTPLogChest.setCreateTime(LocalDateTime.now());
                    serverFTPLogChest.setServerTime(gameplayLogConsumer.parseTimestamp(matcher.group(1), ftpQueueItem));
                    serverFTPLogChest.setType(matcher.group(2).trim().equalsIgnoreCase("Bury") ? 1 : 2);
                    serverFTPLogChest.setChestId(Long.parseLong(matcher.group(3)));
                    Player ownerPlayer = gameplayLogConsumer.getOrInsertPlayerServer(Long.parseLong(matcher.group(4)), serverFTPLogChest.getServerId(), matcher.group(6));
                    serverFTPLogChest.setOwnerPlayerId(ownerPlayer.getId());
                    serverFTPLogChest.setOwnerPlayerSteamId(ownerPlayer.getSteamId());
                    serverFTPLogChest.setLocation(matcher.group(7) + " " + matcher.group(8) + " " + matcher.group(9));
                    serverFTPLogChest.setLocationZone(gameMapZoneUtil.getZone(Double.parseDouble(matcher.group(7)), Double.parseDouble(matcher.group(8))));
                    Player operatorPlayer = gameplayLogConsumer.getOrInsertPlayerServer(Long.parseLong(matcher.group(11)), serverFTPLogChest.getServerId(), matcher.group(13));
                    serverFTPLogChest.setOperatorPlayerId(operatorPlayer.getId());
                    serverFTPLogChest.setOperatorPlayerSteamId(operatorPlayer.getSteamId());
                    chests.add(serverFTPLogChest);
                } else {
                    gameplayLogConsumer.commitDiscardLog(ftpQueueItem, line, "chest no match");
                }
            }
            chests.forEach(serverFTPLogChestMapper::insert);

            List<PlayerAlarm> alarms = Lists.newArrayList();
            if (getCurrentAlarmConfig(ftpQueueItem).getChestEnabled()) {
                List<Long> hasAlarmPackagePlayerIds = getHasAlarmPackagePlayerIds(ftpQueueItem);
                if (!hasAlarmPackagePlayerIds.isEmpty()) {
                    chests.stream().filter(item -> hasAlarmPackagePlayerIds.contains(item.getOwnerPlayerId()) && item.getType() == 2).forEach(item -> {
                        PlayerAlarm playerAlarm = new PlayerAlarm();
                        playerAlarm.setServerId(item.getServerId());
                        playerAlarm.setPlayerId(item.getOwnerPlayerId());
                        playerAlarm.setDetail("chest table");
                        playerAlarm.setLogId(item.getId());
                        playerAlarm.setLogTime(item.getServerTime());
                        playerAlarm.setServerName(ftpQueueItem.getServer().getName());
                        playerAlarm.setAlarmDetail("挖出");
                        playerAlarm.setAlarmItem(item.getLocationZone() + "-箱子");
                        alarms.add(playerAlarm);
                    });
                }
            }
            commitAlarmTask(alarms);
        } catch (Exception e) {
            log.error("箱子日志出现异常", e);
        }
    }
}
